package com.olive.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;

import com.olive.request.NewContentCachingRequestWrapper;

public class ContentCachingRequestWrapperFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		String contentType = request.getContentType();
		if (request instanceof HttpServletRequest) {
			HttpServletRequest requestWrapper = new NewContentCachingRequestWrapper((HttpServletRequest) request);
			if (contentType != null && contentType.contains(MediaType.MULTIPART_FORM_DATA_VALUE)) {
				chain.doFilter(request, response);
			} else {
				chain.doFilter(requestWrapper, response);
			}
			return;
		}
		chain.doFilter(request, response);
		
//		if (request instanceof HttpServletRequest) {
//            if (request.getContentType() != null && !request.getContentType().contains(MediaType.MULTIPART_FORM_DATA_VALUE)) {
//            	HttpServletRequest wrapper = new NewContentCachingRequestWrapper((HttpServletRequest) request);
//                //测试
//                chain.doFilter(wrapper, response);
//                return;
//            }
//        }
//        chain.doFilter(request, response);
	}
}
